﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Microsoft.Extensions.Logging;
using Prism.Commands;
using Prism.Events;
using Prism.Regions;
using YF_Music.Core;
using YF_Music.Core.Caches;
using YF_Music.Core.Enums;
using YF_Music.Core.Events;
using YF_Music.Core.Models;
using YF_Music.Core.Mvvm;
using YF_Music.Core.Services;


namespace YF_Music.Modules.APP_My.ViewModels
{
  public class LocalMusicViewModel : RegionViewModelBase
  {
    private readonly PublishCoreEvent publishEvent;
    private readonly IMusicPlayerService musicPlayerService;
    private readonly IEventAggregator eventAggregator;
    private readonly ILogger<LocalMusicViewModel> logger;

    public DelegateCommand SongSelectedCommand { get; private set; }

    private MusicItem _selectedMusicItem;


    private int _selectedIndex = 0;

    public int SelectedIndex
    {
      get { return _selectedIndex; }
      set { SetProperty(ref _selectedIndex, value); }
    }

    public ObservableCollection<string> TabOptions { get; } = ["歌曲", "歌手", "专辑"];
    public int CurrentTabIndex { get; set; } = 0;

    public DelegateCommand<int?> TabClickedCommand { get; private set; }
    public DelegateCommand CancelCommand { get; private set; }

    private readonly string cacheName = "LocalMusic";

    private readonly PlaylistSession playlistSession;
    public LocalMusicViewModel(IRegionManager regionManager, PublishCoreEvent publishEvent, IMusicPlayerService musicPlayerService, IEventAggregator eventAggregator,ILogger<LocalMusicViewModel> logger) : base(regionManager)
    {
      CancelCommand = new DelegateCommand(CancalLocalMusic);
      TabClickedCommand = new DelegateCommand<int?>(OnTabClicked);
      playlistSession = new PlaylistSession(cacheName, @"C:\Users\Zero\Downloads");
      LocalSongs = new ObservableCollection<MusicItem>([.. playlistSession.Items]);

      #region MyRegion
      //ObservableCollection<MusicItem> list2 = [
      //  new MusicItem() { ID = 1, Title = "Promise", Downloaded = true, SoundQuality = Enm_ToneQuality.SQ,Author="叶峰" },
      //  new MusicItem() { ID = 2, Title = "今天有朵云爱我", Downloaded = true, SoundQuality = Enm_ToneQuality.SQ,Author="李宇春" },
      //  new MusicItem() { ID = 3, Title = "如果当时", Downloaded = false, SoundQuality = Enm_ToneQuality.SQ,Author="许嵩" },
      //  new MusicItem() { ID = 4, Title = "庐州月", Downloaded = true, SoundQuality = Enm_ToneQuality.SQ,Author="许嵩" },
      //    new MusicItem() { ID = 1, Title = "Promise", Downloaded = true, SoundQuality = Enm_ToneQuality.SQ,Author="叶峰" },
      //  new MusicItem() { ID = 2, Title = "今天有朵云爱我", Downloaded = true, SoundQuality = Enm_ToneQuality.SQ,Author="李宇春" },
      //  new MusicItem() { ID = 3, Title = "如果当时", Downloaded = false, SoundQuality = Enm_ToneQuality.SQ,Author="许嵩" },
      //  new MusicItem() { ID = 4, Title = "庐州月", Downloaded = true, SoundQuality = Enm_ToneQuality.SQ,Author="许嵩" },
      //    new MusicItem() { ID = 1, Title = "Promise", Downloaded = true, SoundQuality = Enm_ToneQuality.SQ,Author="叶峰" },
      //  new MusicItem() { ID = 2, Title = "今天有朵云爱我", Downloaded = true, SoundQuality = Enm_ToneQuality.SQ,Author="李宇春" },
      //  new MusicItem() { ID = 3, Title = "如果当时", Downloaded = false, SoundQuality = Enm_ToneQuality.SQ,Author="许嵩" },
      //  new MusicItem() { ID = 4, Title = "庐州月", Downloaded = true, SoundQuality = Enm_ToneQuality.SQ,Author="许嵩" }
      //  ];
      //LocalSongs.AddRange(list2); 
      #endregion

      this.publishEvent = publishEvent;
      this.musicPlayerService = musicPlayerService;
      this.eventAggregator = eventAggregator;
      this.logger = logger;
      SongSelectedCommand = new DelegateCommand(PlayMusic);
      eventAggregator.GetEvent<AppCloseEvent>().Subscribe(APPClosing);
    }

    private void APPClosing()
    {
      playlistSession.Dispose();
      logger.LogInformation("APPClosing");
    }

    private void PlayMusic()
    {
      if (SelectedIndex < 0)
      {
        return;
      }
      if (!musicPlayerService.IsListEqual(LocalSongs))
      {
        musicPlayerService.LoadPlaylist(LocalSongs, playlistSession);
      }
      musicPlayerService.Play(SelectedIndex);
    }

    private void CancalLocalMusic()
    {
      RegionManager.RequestNavigate(RegionNames.ContentRegion, App_My_ViewNames.APP_MyView);
      //publishEvent.PublishShowOnlyPlayEvent(false);
    }

    public ObservableCollection<MusicItem> LocalSongs { get; private set; }
    private void OnTabClicked(int? index)
    {
      if (index.HasValue)
      {
        MessageBox.Show($"选中{index}");
      }
    }
  }
}
